package hu.codelab.lg.servicecenter.activity;

import java.util.List;

import hu.codelab.lg.servicecenter.R;
import hu.codelab.lg.servicecenter.ServiceCenterApplication;
import hu.codelab.lg.servicecenter.adapters.PhonesAdapter;
import hu.codelab.lg.servicecenter.entities.Address;
import hu.codelab.lg.servicecenter.entities.Phone;
import hu.codelab.lg.servicecenter.entities.Phones;
import hu.codelab.lg.servicecenter.entities.ServiceCenter;
import hu.codelab.lg.servicecenter.widget.LGDialog;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Region;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public final class DetailedActivity extends Activity {

    private static final int DIALOG_NO_CONNECTION = 0;
    private static final int DIALOG_PHONES = 1;

    protected String primaryPhoneNumber;

    protected Phones phones;

    private Typeface font;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ServiceCenter serviceCenter = ((ServiceCenterApplication) getApplication()).getCurrentServiceCenter();
        final Address address = serviceCenter.getAddress();
        phones = serviceCenter.getPhones();

        final Resources resources = getResources();

        font = Typeface.createFromAsset(getAssets(), "helvetica_neue.ttf");

        final RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_detailed, null);
        setContentView(layout);

        final TextView nameText = (TextView) layout.findViewById(R.id.tv_detailed_name);
        final TextView addressText = (TextView) layout.findViewById(R.id.tv_detailed_address);
        final TextView phonesText = (TextView) layout.findViewById(R.id.tv_detailed_phones);
        final TextView onlineText = (TextView) layout.findViewById(R.id.tv_detailed_online);
        final Button callButton = (Button) layout.findViewById(R.id.b_detailed_call);
        final Button mapButton = (Button) layout.findViewById(R.id.b_detailed_map);
        final TextView leadView = (TextView) layout.findViewById(R.id.tv_header_lg);
        final TextView titleView = (TextView) layout.findViewById(R.id.tv_header_view);
        
        leadView.setTypeface(font);
        titleView.setTypeface(font);
        nameText.setTypeface(font);
        addressText.setTypeface(font);
        phonesText.setTypeface(font);
        onlineText.setTypeface(font);
        callButton.setTypeface(font);
        mapButton.setTypeface(font);

        nameText.setText(serviceCenter.getName());
        addressText.setText(address.getZip() + " " + address.getCity() + ", " + address.getStreet() + " " + address.getNumber());

        if (phones != null && phones.getPhoneItems() != null && !phones.getPhoneItems().isEmpty()) {
            final StringBuilder phonesBuilder = new StringBuilder();
            final List<Phone> phoneItems = phones.getPhoneItems();

            int type;
            Phone phone;
            for (int i = 0; i < phoneItems.size(); i++) {
                phone = phoneItems.get(i);
                type = phone.getType();
                switch (type) {
                    case 0:
                        phonesBuilder.append("M\t\t");
                        break;
                    case 1:
                        phonesBuilder.append("T\t\t");
                        break;
                    case 2:
                        phonesBuilder.append("F\t\t");
                        break;
                    default:
                        break;
                }
                phonesBuilder.append(phone.getNumber());

                if (i < phoneItems.size() - 1) {
                    phonesBuilder.append('\n');
                }

                if (i == 0) {
                    primaryPhoneNumber = phone.getNumber();
                }
            }
            phonesText.setText(phonesBuilder);
        }

        onlineText.setText(serviceCenter.getEmail());
        onlineText.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType("plain/text");
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { serviceCenter.getEmail() });
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "LG szervizeléssel kapcsolatos kérdés");
                startActivity(Intent.createChooser(emailIntent, ""));
            }
        });

        callButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View view) {
                if (phones != null && phones.getPhoneItems() != null && phones.getPhoneItems().size() > 1) {
                    showDialog(DIALOG_PHONES);
                } else {
                    final Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:" + primaryPhoneNumber));
                    startActivity(callIntent);
                }
            }

        });

        mapButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View view) {
                if (ServiceCenterApplication.isNetworkAvailable(DetailedActivity.this)) {
                    startActivity(new Intent(DetailedActivity.this, LGMapActivity.class));
                } else {
                    showDialog(DIALOG_NO_CONNECTION);
                }
            }

        });

        final TextView headerView = (TextView) layout.findViewById(R.id.tv_header_view);
        headerView.setTypeface(font);
        try {
            headerView.setText(resources.getString(getPackageManager().getActivityInfo(getComponentName(), 0).labelRes));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;

        switch (id) {
            case DIALOG_NO_CONNECTION:
                dialog = new LGDialog(DetailedActivity.this, R.style.Theme_Dialog_Translucent);
                dialog.setContentView(R.layout.dialog_infos);
                TextView lorem = (TextView) dialog.findViewById(R.id.tv_dialog_infos_lorem);
                TextView title = (TextView) dialog.findViewById(R.id.tv_dialog_infos_title);
                ImageButton close = (ImageButton) dialog.findViewById(R.id.ib_dialog_infos_close);

                lorem.setTypeface(font);
                title.setTypeface(font);
                title.setText("Hálózat nem elérhető");

                close.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(final View view) {
                        removeDialog(DIALOG_NO_CONNECTION);
                    }

                });

                break;
            case DIALOG_PHONES:
                dialog = new LGDialog(DetailedActivity.this, R.style.Theme_Dialog_Translucent);
                dialog.setContentView(R.layout.dialog_phone_select);
                close = (ImageButton) dialog.findViewById(R.id.ib_dialog_phones_close);
                close.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(final View view) {
                        removeDialog(DIALOG_PHONES);
                    }

                });

                final ListView phoneList = (ListView) dialog.findViewById(R.id.lv_dialog_phones);
                phoneList.setAdapter(new PhonesAdapter(DetailedActivity.this, phones.getPhoneItems()));
                phoneList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long itemId) {
                        dismissDialog(DIALOG_PHONES);
                        final Intent callIntent = new Intent(Intent.ACTION_CALL);
                        callIntent.setData(Uri.parse("tel:" + ((Phone) parent.getAdapter().getItem(position)).getNumber()));
                        startActivity(callIntent);
                    }

                });

                break;
            default:
                break;
        }

        if (dialog != null && dialog instanceof LGDialog) {
            final RelativeLayout dialogLayout = (RelativeLayout) dialog.findViewById(R.id.rl_dialog);

            ((LGDialog) dialog).setOnTouchEventListener(new LGDialog.OnTouchEventListener() {

                @Override
                public void onTouchEvent(final Dialog diag, final MotionEvent event) {
                    final Region dialogRegion = new Region(dialogLayout.getLeft(), dialogLayout.getTop(), dialogLayout.getRight(), dialogLayout.getBottom());

                    if (dialogRegion.contains((int) event.getX(), (int) event.getY())) {
                        return;
                    }

                    diag.dismiss();
                }

            });
        }

        return dialog;
    }

    @SuppressWarnings("unused")
    public void back(final View view) {
        finish();
    }
    

    @SuppressWarnings("unused")
    public void mainView(final View view) {
        startActivity(new Intent(getBaseContext(), MainActivity.class));
    }
}
